import 'dart:convert';

import 'package:freedom/page/mahjong/bloc/room_info.dart';
import 'package:freedom/page/mahjong/common/message_content.dart';

class MahjongRoomRecord {
  List<MahjongMessage>? actions;
  RoomInfo? roomInfo;
  MahjongRoomRecord({this.actions, this.roomInfo});
  factory MahjongRoomRecord.formJSON(Map<String, dynamic> json) {
    List actions = json["action"] is String ? jsonDecode(json["action"]) : [];
    List<MahjongMessage> actionList = [];
    for (var element in actions) {
      actionList.add(MahjongMessage.fromJson(element));
    }
    Map<String, dynamic> room =
        json["roomInfo"] is String ? jsonDecode(json["roomInfo"]) : {};
    return MahjongRoomRecord(
        actions: actionList, roomInfo: RoomInfo.formJSON(room));
  }

  static List<MahjongRoomRecord> build(dynamic data) {
    List<MahjongRoomRecord> roomRecord = [];
    List recoderList = data is List ? data : [];
    for (var element in recoderList) {
      roomRecord.add(MahjongRoomRecord.formJSON(element));
    }
    return roomRecord;
  }
}
